package com.asen.commons.core.jdbc.factory;

import com.asen.commons.core.jdbc.Jdbc;
import com.asen.commons.core.jdbc.impl.JdbcImpl;
import com.asen.commons.utils.BaseAssert;
import com.asen.commons.utils.SpringUtil;

import javax.sql.DataSource;

/**
 * jdbc工厂
 *
 * @author Gary
 * @version 1.0
 * @date 2018-11-28
 */
public class JdbcFactory {

    /**
     * 创建MySql的Jdbc连接
     *
     * @return
     */
    public static Jdbc createJdbc() {
        Jdbc jdbc = SpringUtil.getBean(Jdbc.class);
        if (jdbc == null) {
            jdbc = new JdbcImpl();
            DataSource dataSource = SpringUtil.getBean(DataSource.class);
            BaseAssert.notNull(dataSource, "数据源dataSource不存在！");
            jdbc.setDataSource(dataSource);
        }
        return jdbc;
    }

    /**
     * 创建MySql的Jdbc连接
     *
     * @param dataSource
     * @return
     */
    public static Jdbc createJdbc(DataSource dataSource) {
        Jdbc jdbc = SpringUtil.getBean(Jdbc.class);
        if (jdbc == null) {
            jdbc = new JdbcImpl();
        }
        jdbc.setDataSource(dataSource);
        return jdbc;
    }
}
